package it.eng.area118.mdocommon.log;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Semplice implementazione di un logger
 * 
 * @author Mauro Miranda
 * 
 */
public class ConsoleLogger implements Log {

	public static final Level OFF = new Level(0, "OFF");
	public static final Level ERROR = new Level(1, "ERROR");
	public static final Level WARN = new Level(2, "WARN");
	public static final Level INFO = new Level(3, "INFO");
	public static final Level DEBUG = new Level(4, "DEBUG");

	public static final Level[] levels = { OFF, ERROR, WARN, INFO, DEBUG };

	protected static DateFormat format = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");

	protected Class clazz;
	protected static Level defeultLevel = INFO;

	public static void setDefaultLevel(Level defLevel) {
		defeultLevel = defLevel;
	}

	public static void setDefaultLevel(String defLevel) {
		for (int i = 0; i < levels.length; i++) {
			if (levels[i].name.equals(defLevel)) {
				setDefaultLevel(levels[i]);
			}
		}
	}

	public static ConsoleLogger getInstance(Class class1) {
		return new ConsoleLogger(class1);
	}

	protected ConsoleLogger(Class class1) {
		this.clazz = class1;
	}

	public synchronized void log(Level level, String message, Throwable t) {
		if (level.value <= defeultLevel.value) {
			String m = format.format(new Date()) + " - [" + level.name + "]\t- " + message;
			System.out.println(m);
			if (t != null) {
				t.printStackTrace(System.out);
			}
		}
	}

	public void error(String string) {
		error(string, null);
	}

	public void error(String string, Throwable e) {
		log(ERROR, string, e);
	}

	public void info(String string) {
		log(INFO, string, null);
	}

	public void debug(String string) {
		debug(string, null);
	}

	public void debug(String string, Exception ex) {
		log(DEBUG, string, ex);
	}

	public void warn(String string) {
		warn(string, null);
	}

	public void warn(String string, Throwable e) {
		log(WARN, string, e);
	}

	public static class Level {
		private int value;
		private String name;

		public Level(int value, String name) {
			super();
			this.value = value;
			this.name = name;
		}

	}

	public void warn(String msg, Exception e) {
		log(WARN, msg, e);

	}

	public void error(String msg, Exception e) {
		log(ERROR, msg, e);
	}

	public void start() {
		// CREME Auto-generated method stub

	}

	public void close() {
		// CREME Auto-generated method stub

	}

	public void reset() {
		// CREME Auto-generated method stub

	}

	public void setLevel(String level) {

	}

	public static void main(String[] args) {
		Logger.init("terminal118", Log.INFO, Logger.CONSOLE, 10);
		Log log = Logger.getInstance(null);
		log.warn("WARNINGGGGGGGG");
		log.warn("WARNINGGGGGGGG");
	}
}
